package com.sina.shop.goods;

import com.sina.shop.domain.goods.Goods;
import com.sina.shop.service.goods.GoodsService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-08-11:08
 */
@RestController
@RequestMapping("/goods")
public class GoodsController extends BaseController {

    @Autowired
    @Qualifier("goodsService")
    private GoodsService goodsService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getGoodsList", method = RequestMethod.GET)
    public Map<String, Object> getGoodsList(@RequestBody(required = false) Goods goods) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getGoodsList, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        Map map = new HashMap(4);
        Long count[] = new Long[1];
        List<Goods> list = goodsService.getGoodsList(goods, count);
        map.put("count", count[0]);
        map.put("list", list);

        return map;
    }

    @RequestMapping(value = "/getGoods", method = RequestMethod.POST)
    public Goods getGoods(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsService.getGoods(id);
    }

    @RequestMapping(value = "/deleteGoods", method = RequestMethod.POST)
    public Integer deleteGoods(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsService.deleteGoods(id);
    }

    @RequestMapping(value = "/saveGoods", method = RequestMethod.POST)
    public Goods saveGoods(@RequestBody(required = false) Goods goods) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsService.saveGoods(goods);
    }

    @RequestMapping(value = "/updateGoods", method = RequestMethod.POST)
    public Goods updateGoods(@RequestBody(required = false) Goods goods) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateGoods, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return goodsService.updateGoods(goods);
    }
}
